package cn.miao.hrm.controller;

import cn.miao.hrm.dto.RegisterUserDto;
import cn.miao.hrm.dto.SendSMSCode;
import cn.miao.hrm.service.IVipUserService;
import cn.miao.hrm.domain.VipUser;
import cn.miao.hrm.query.VipUserQuery;
import cn.miao.util.AjaxResult;
import cn.miao.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/vipUser")
public class VipUserController {
    @Autowired
    public IVipUserService vipUserService;


    /**
     * 生成图片验证码
     *
     * @param key 关键
     * @return {@link AjaxResult}
     */
    @GetMapping("/verifycode/imageCode/{key}")
    public AjaxResult imageCode(@PathVariable("key") String key){
        return vipUserService.createImgCode(key);
    }

    /**
     * 发送短信代码
     *
     * @param dto dto
     * @return {@link AjaxResult}
     */
    @PostMapping("/verifycode/sendSmsCode")
    public AjaxResult sendSmsCode(@Valid @RequestBody SendSMSCode dto){
        return vipUserService.sendSmsCode(dto);
    }

    /**
     * 注册
     *
     * @param dto dto
     * @return {@link AjaxResult}
     */
    @PostMapping("/user/register")
    public AjaxResult register(@Valid @RequestBody RegisterUserDto dto){
        return vipUserService.register(dto);
    }




    /**
    * 保存和修改
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody VipUser vipUser){
        if(vipUser.getId()!=null){
            vipUserService.updateById(vipUser);
        }else{
            vipUserService.insert(vipUser);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        vipUserService.deleteById(id);
        return AjaxResult.me();
    }

   /**
   * 根据ID获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj(vipUserService.selectById(id));
    }


    /**
    * 获取所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        return AjaxResult.me().setResultObj(vipUserService.selectList(null));
    }

    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public AjaxResult json(@RequestBody VipUserQuery query){
        Page<VipUser> page = new Page<VipUser>(query.getPage(),query.getRows());
        page = vipUserService.selectPage(page);
        PageList pageList = new PageList<VipUser>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }
}
